package a_doubleWrite;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 17. 电话号码的字母组合
 * 给定一个仅包含数字 2-9 的字符串，返回所有它能表示的字母组合。答案可以按 任意顺序 返回。
 *
 * 给出数字到字母的映射如下（与电话按键相同）。注意 1 不对应任何字母。
 * @author: luolidong
 * @create: 2021-07-19 09:04
 * @version: 1.0
 */
public class L17 {
    public List<String> letterCombinations(String digits) {
        List<String> list=new ArrayList<>();
        Map<Character,String> map= new HashMap<>();
        map.put('2',"abc");
        map.put('3',"def");
        map.put('4',"ghi");
        map.put('5',"jkl");
        map.put('6',"mno");
        map.put('7',"pqrs");
        map.put('8',"tuv");
        map.put('9',"wxyz");

        dfs(digits,map,0,list,new StringBuilder());
        return list;
    }

    private void dfs(String digits, Map<Character, String> map, int index, List<String> list, StringBuilder stringBuilder) {
        if(stringBuilder.length()==digits.length()){
            list.add(stringBuilder.toString());
            return;
        }
        char c = digits.charAt(index);
        String s = map.get(c);
        for (int i = 0; i <s.length() ; i++) {
            stringBuilder.append(s.charAt(i));
            dfs(digits, map, index+1, list, stringBuilder);
            stringBuilder.deleteCharAt(stringBuilder.length()-1);
        }
    }


}
